package com.zzxz.practicaltrainingsys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzxz.practicaltrainingsys.entity.Datafile;
import com.zzxz.practicaltrainingsys.mapper.DatafileMapper;
import com.zzxz.practicaltrainingsys.mapper.MemberMapper;
import com.zzxz.practicaltrainingsys.service.DatafileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.zzxz.practicaltrainingsys.component.WebSocketServiceHelper.teamMapper;

@Service
public class DatafileServiceImpl implements DatafileService {
    @Autowired
    private DatafileMapper datafileMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public List<String> getDatafileTypes() {
        List<String> Datafile = datafileMapper.getDatafileTypes();
        return Datafile;
    }

    @Override
    public List<Datafile> getDatafileInfo() {
        List<Datafile> datafileInfoList = datafileMapper.getDatafileInfo();
        return datafileInfoList;
    }

    @Override
    public int insertDatafile(Datafile datafile) {
        // 打印存储的文件名
        System.out.println("Storing file name: " + datafile.getDataUrl());
        return datafileMapper.insertDatafile(datafile);
    }

    @Override
    public int deleteDatafileById(Integer data_id) {
        return datafileMapper.deleteDatafileById(data_id);
    }

    @Override
    public List<Map<String, Object>> getAllDataFiles() {
        return datafileMapper.getAllDataFiles();
    }

    @Override
    public PageInfo<Map<String, Object>> findDatafilePages(Integer pageNum, Integer pageSize) {
        PageHelper.startPage( pageNum,pageSize);
        List<Map<String, Object>> lists = datafileMapper.getAllDataFiles();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    //实训圈插入文件
    @Override
    public int circleInsertDatafile(Datafile datafile) {
        return datafileMapper.circleInsertDatafile(datafile);
    }

    @Override
    //查询学生所在的小组Id
    public List<Map<String, Object>> getDataFileByStudent(Integer student_id) {
        Integer team_id = memberMapper.getTeamIdByStudentId(student_id);
        if (team_id == null) {
            return Collections.emptyList();
        }
        //查询小组对应的项目id
        Integer project_id = teamMapper.getProjectIdByTeamId(team_id);
        if (project_id == null) {
            return Collections.emptyList();
        }
        //查询该项目下的文件
        return datafileMapper.getDataFileByStudent(project_id);
    }

    @Override
    public List<Map<String, Object>> getDataFilesByTeacherAndProject(Integer teacher_id, Integer project_id) {
        return datafileMapper.getDataFilesByTeacherAndProject(teacher_id, project_id);
    }

    @Override
    public List<Map<String, Object>> getDataFilesByProject(Integer project_id) {
        return datafileMapper.getDataFilesByProject(project_id);
    }


}
